
class ShiShen:
    """
    十神：只使用get_item返回指定的对象
    """
    _cache = {}
    _inited = False

    def __new__(cls, same_yy, distance, name):
        if cls._inited:
            raise RuntimeError(f'{cls.__name__} 已经加载，不能直接实例化，请使用 {cls.__name__}.get_item(same_yy, dist)')
        key = (same_yy, distance)
        if key in cls._cache:
            return cls._cache[key]
        else:
            self = super().__new__(cls)
            cls._cache[key] = self
            return self

    def __init__(self, same_yy, distance, name):
        self.same_yy = same_yy
        self.distance = distance
        self.name = name

    @classmethod
    def init_shishen(cls, items):
        """
        初始化的时候调用一次
        """
        for distance, same_yy, name in items:
            cls(same_yy, distance, name)
        cls._inited = True

    @classmethod
    def get_item(cls, same_yy, distance):
        """
        返回指定的对象
        """
        return cls._cache[(same_yy, distance)]

    @classmethod
    def get_item_by_name(cls, name):
        for k, god in cls._cache.items():
            if god.name == name:
                return god

    def __str__(self):
        return self.name
